/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel;

import org.apache.commons.io.IOUtils;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.io.InputStream;

/**
 * Utility class for test cases
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 21:54 18.11.11
 *
 * @author Peter Herber
 * @version 1.0
 */
public class RestServiceUtil {

    /** REST service base url */
    private static final String REST_INTERFACE_BASE_URL = "http://localhost:8080/retabel/services/";

    /**
     * Retrieve the REST service base uri builder
     *
     * @return uri builder instance targeting the REST service base url
     */
    public static UriBuilder getRestInterfaceUriBuilder() {
        return UriBuilder.fromUri(REST_INTERFACE_BASE_URL);
    }

    /**
     * Retrieves a classpath resource as byte array
     *
     * @param resource the name of resource (relative to the current class package)
     * @return the bytes of the resource
     * @throws java.io.IOException if the resource is missing
     */
    public static byte[] getResourceContentAsBytes(String resource) throws IOException {
        InputStream resourceInputStream = null;
        try {
            resourceInputStream = RestServiceUtil.class.getResourceAsStream(resource);

            if (resourceInputStream == null) {
                throw new IOException("missing resource: "+resource);
            }

            return IOUtils.toByteArray(resourceInputStream);
        }
        finally {
            if (resourceInputStream != null) {
                resourceInputStream.close();
            }
        }
    }

}
